#include"Stack.h"
void StackInit(Stack* ps) {
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

void StackPush(Stack* ps, DataType x) {
	if (ps->capacity == ps->size) {
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		DataType* tmp = (int*)realloc(ps->a, newCapacity * sizeof(DataType));
		if (tmp == NULL) {
			perror("realloc fail");
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->size++] = x;
}

void StackPop(Stack* ps) {
	assert(ps->size);
	ps->size--;
}

DataType StackTop(Stack* ps) {
	assert(ps->size);
	return ps->a[ps->size-1];
}

bool StackEmpty(Stack* ps) {
	return ps->size == 0;
}

void StackPrint(Stack st) {
	for (int i = 0; i < st.size; i++) {
		printf("%d ", st.a[i]);
	}
	printf("\n");
}

void StackDestory(Stack* ps) {
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}